//
//  HXDLViewController.m
//  A Learning Walk
//
//  Created by wudongyang on 16/6/27.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "HXDLViewController.h"

@interface HXDLViewController ()
@property (retain, nonatomic) IBOutlet UITextField *HXZHText;
@property (retain, nonatomic) IBOutlet UITextField *HXMMText;
- (IBAction)HXDLButton:(id)sender;
- (IBAction)HXZCButton:(id)sender;
@property (nonatomic, copy)NSString *userNames;
@property (nonatomic, copy)NSString *password;
- (IBAction)ReturnButton:(id)sender;

@end

@implementation HXDLViewController
- (void)dealloc {
    [_HXZHText release];
    [_HXMMText release];
    [_userNames release];
    [_password release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // TextField输入框中变化就会走的方法,实时取到text值
    [self.HXZHText addTarget:self action:@selector(textAction:) forControlEvents:UIControlEventEditingChanged];
    // TextField输入框中变化就会走的方法,实时取到text值
    [self.HXMMText addTarget:self action:@selector(textAction1:) forControlEvents:UIControlEventEditingChanged];

}

- (void)textAction:(UITextField *)text{
    // 将注册账号赋值给self.userNames
    self.userNames = text.text;
}
- (void)textAction1:(UITextField *)text{
    // 将注册密码赋值给self.password
    self.password = text.text;
}
// 登录按钮点击方法
- (IBAction)HXDLButton:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // 环信用户登录的方法(需要参数1:账号 需要参数2:密码)
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userNames password:self.password completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error) {
            NSLog(@"%@",loginInfo);
            NSLog(@"登录成功");
            [defaults setObject:@"1" forKey:@"isLogin"];
            //存入网盘
            [defaults synchronize];
            [[[EaseMob sharedInstance] chatManager] setIsAutoLoginEnabled:YES];
//            self.userBlock(self.userNames);
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            
        } else {
            NSLog(@"%@",error);
            if ([error.description isEqualToString:@"用户名或密码错误."] || [error.description isEqualToString:@"User name or password is incorrect."]) {
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"用户名或密码错误" message:nil preferredStyle:UIAlertControllerStyleAlert];
                
                [alertC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }]];
                [self presentViewController:alertC animated:YES completion:^{
                    
                    
                }];
            }
        }
        
    } onQueue:nil];
}
// 注册按钮点击方法
- (IBAction)HXZCButton:(id)sender {
    
        // 将注册账号赋值给self.userNames
        self.userNames = self.HXZHText.text;
        // 将注册密码赋值给self.password
        self.password = self.HXMMText.text;
    

            // 环信注册的方法(需要参数1:账号 需要参数2:密码)
            [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.userNames password:self.password withCompletion:^(NSString *username, NSString *password, EMError *error) {
                // 注册成功走的方法
                if (!error) {
                    NSLog(@"注册成功");
                    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"注册成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
                    
                    [alertC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    }]];
                    [self presentViewController:alertC animated:YES completion:^{
                        
                        
                    }];
                    
                    // 注册失败走这个方法
                } else {
                    NSLog(@"%@",error);
                    // 判断环信返回的注册失败的错误信息error.description
                    if ([error.description isEqualToString:@"用户已存在."] || [error.description isEqualToString:@"Username already exists."]) {
                        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户已存在" preferredStyle:UIAlertControllerStyleAlert];
                        
                        [alertC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertC animated:YES completion:^{
                            
                            
                        }];
                        
                    }else if ([error.description isEqualToString:@"用户名不能为空."] || [error.description isEqualToString:@"Username cannot be empty."]){
                        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
                        
                        [alertC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertC animated:YES completion:^{
                            
                            
                        }];
                    }else if ([error.description isEqualToString:@"密码不能为空."] || [error.description isEqualToString:@"Password cannot be empty."]){
                        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
                        
                        [alertC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertC animated:YES completion:^{
                            
                            
                        }];
                    }
                }
                
            } onQueue:nil];
            NSLog(@"注册成功的用户名:%@",self.userNames);
            NSLog(@"注册成功的密码:%@",self.password);
            if (self.password) {
//                NSLog(@"注册成功");
            }else{

            };
        }

// 返回到上级页面
- (IBAction)ReturnButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
@end
